package com.fc.design.singleton;

/**
 * <p> Author：lidongxu </p>
 * <p> Title：</p>
 * <p> Description：</p>
 * <p> Create Time：2020-03-30 11:15 </p>
 */
public class TestSinleton {
    public static void main(String[] args) {
        President zt1 = President.getInstance();
        zt1.getName();    //输出总统的名字
        President zt2 = President.getInstance();
        zt2.getName();    //输出总统的名字
        if (zt1 == zt2) {
            System.out.println("他们是同一人！");
        } else {
            System.out.println("他们不是同一人！");
        }
    }
}

class President {
    private static President instance = null;

    private President(){
        System.out.println("New");
    }

    public static synchronized President getInstance() {
        if (instance == null)
            instance = new President();
        else
            System.out.println("Exist");
        return instance;
    }

    public void getName() {
        System.out.println("我是川建国");
    }
}
